<?php
/**
 * @see Epic_Validate_Element_Interface
 */
require 'Epic/Validate/Element/Interface.php';

abstract class Epic_Validate_Element_Abstract implements Epic_Validate_Element_Interface
{
	/**
     * Value valid
     * @var mixed
     */
    protected $_value = '';
    
    /**
     * Filter data
     * @var int
     */
    protected $_filter = '';
    
    /**
     * Option
     * @var array
     */
    protected $_options = null;
    
    /**
     * Error message
     * @var string
     */
    protected $_errorMessage = '';
    
    /**
     * Prefix for error message
     * @var string
     */
    protected $_errorPrefix = '';
    
    /**
     * Suffix for error message
     * @var string
     */
    protected $_errorSufix = '';

    /**
     * Contructor
     * 
     * @param mixed $value value valid
     * @param array $options
     */
    public function __construct($value = null, array $options = null)
    {
    	if(null !== $value) 
       		$this->setValue($value);
        if(null !== $options)
			$this->setOptions($options);
    }
    
    /**
     * Set validation value
     * 
     * @param mixed $value
     * @throws Exception
     */
    public function setValue($value)
    {
    	if (empty($value)) {
    		throw new Exception('Invalid value for the validator.');
    	}
        $this->_value = $value;
    }
   
    /**
     * Get supplied value
     * @return mixed 
     */
    public function getValue()
    {
        return $this->_value;
    }
   
    /**
     * Set validation options
     * 
     * @param array $options
     * @throws Exception
     */
    public function setOptions(array $options)
    {
    	if (empty($options)) {
    		throw new Exception('Invalid options for the validator.');
    	}
        $this->_options = $options;
    }
   
    /**
     * Get validation options
     * @return array
     */
    public function getOptions()
    {
        return $this->_options;
    }
     
    /**
     * Set the validation filter
     * 
     * @param int $filter
     * @throws Exception
     */
    public function setFilter($filter)
    {
    	if (!is_string($filter)) {
    		throw new Exception('Invalid filter for the validator.');
    	}
        $this->_filter = $filter;
    }
   
    /**
     * Get the validation filter
     * 
     * @return int
     */
    public function getFilter()
    {
        return $this->_filter;
    }
    
    /**
     * Set prefix for error message
     *
     * @param string $errorPrefix
     * @throws ValidatorException
     */
    public function setErrorPrefix($errorPrefix)
    {
        if (!is_string($errorPrefix)) {
            throw new ValidatorException('Invalid error prefix for the validator.');    
        }
        $this->_errorPrefix = $errorPrefix;
    }
   
    /**
     * Retrive prefix for error message
     *
     * @return string
     */
    public function getErrorPrefix()
    {
        return $this->_errorPrefix;
    }
    
    /**
     * Set suffix for error message
     *
     * @param string $errorSufix
     * @throws ValidatorException
     */
    public function setErrorSufix($errorSufix)
    {
        if (!is_string($errorSufix)) {
            throw new ValidatorException('Invalid error sufix for the validator.');    
        }
        $this->_errorSufix = $errorSufix;
    }
   
    /**
     * Retrive suffix for error message
     *
     * @return string
     */
    public function getErrorSufix()
    {
        return $this->_errorSufix;
    }
    
    /**
     * Set the error message
     *
     * @param string $errorMessage
     * @throws ValidatorException
     */
    public function setErrorMessage($errorMessage)
    {
        if (!is_string($errorMessage)) {
            throw new ValidatorException('Invalid error message for the validator.');    
        }
        $this->_errorMessage = $errorMessage;
    }
   
    /**
     * Return a string explaining the reason(s) for validation failure.
     *
     * @return string
     */
    public function getErrorMessage()
    {
    	return $this->getErrorPrefix() . 'The value ' . $this->getValue() . ' is incorrect. ' . $this->_errorMessage . $this->getErrorSufix();	
    }
   
    /**
     * Validate the supplied value
     *
     * @return string
     */
    public function isValid()
    {
        return filter_var($this->getValue(), $this->getFilter(), array('options' => $this->getOptions()));  
    }
} 